package com.example.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.*;

public class Test {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        System.out.println("int数组转字符串");
        text1();
        System.out.println("字符串反转");
        test2();
        System.out.println("第一种方法");
        test3();
        System.out.println("第二种方法");
        test4();
        System.out.println("第三种方法");
        test6();

        System.out.println("====================================");

    }

  public static void text1(){
        int[] arr={2,4,1,7,6};
        String str="";

        StringBuilder stringBuilder=new StringBuilder();
      for(int i=0;i<arr.length;i++)

      {

          if(i!=arr.length-1)

              stringBuilder.append(arr[i]+" ,");

          else

              stringBuilder.append(arr[i]+" ");

      }
      System.out.println(stringBuilder);
  }

  public static void test2(){
      Scanner scanner = new Scanner(System.in);
      System.out.println("输入一个字符串");
      String str=scanner.nextLine();
      StringBuilder builder = new StringBuilder(str);
      StringBuilder stringBuilder=builder.reverse();
      System.out.println(stringBuilder);
  }

  public static class Threads extends Thread{
        public void run(){
            for (int i = 0; i < 20; i++) {
                System.out.println("我会run方法"+i);
            }
        }
  }

  public static void test3  (){
      Threads threads = new Threads();
      threads.start();
      for (int i = 0; i < 200; i++) {
          System.out.println("-------------------------");
      }
  }


    public static class RaceImplent implements Runnable {
        private static  String winner ;

        @Override
        public void run() {

            for (int i = 0; i <= 100; i++) {
                if (Thread.currentThread().getName().equals("兔子") && i==60 ){
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if (isOver(i)){
                    return;
                }
                System.out.println(Thread.currentThread().getName()+"跑了"+i+"步");
            }
        }

        boolean isOver(int  step){
            if(winner != null){
                return true;
            }else if(step == 100){
                winner = Thread.currentThread().getName();
                System.out.println("冠军是:"+Thread.currentThread().getName());
            }
            return false;
        }



    }
    public static void test4() {
        RaceImplent raceImplent = new RaceImplent();
        new Thread(raceImplent,"兔子").start();
        new Thread(raceImplent,"乌龟").start();
    }



    public static class CallableTest implements Callable<List<String>> {
        @Override
        public List<String> call() throws Exception {
            System.out.println(Thread.currentThread().getName());
            ArrayList<String> objects = new ArrayList<>();
            objects.add(Thread.currentThread().getName());
            System.out.println("执行了....");
            return objects;
        }


    }
    public static void test6() throws ExecutionException, InterruptedException {
        CallableTest call1 = new CallableTest();
        CallableTest call2 = new CallableTest();
        /**创建线程池对象*/
        ExecutorService service = Executors.newFixedThreadPool(2);

        //可以执行Runnable对象或者Callable对象代表的线程
        Future<List<String>> res1 = service.submit(call1);
        Future<List<String>> res2 = service.submit(call2);

        //get获取线程处理结果
        List<String> list = res1.get();
        System.out.println(list);
        List<String> list2 = res2.get();
        System.out.println(list2);
    }

}
